package api

import (
	"gateway/basic/config"
	user "gateway/basic/proto/userproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// GetSecurityEvents 获取安全事件列表
// @Summary 获取安全事件列表
// @Description 获取安全事件列表，支持分页和筛选
// @Tags Security
// @Accept json
// @Produce json
// @Param type query string false "事件类型: login, withdrawal, payment, system"
// @Param severity query string false "严重程度"
// @Param status query string false "状态: pending, handled, ignored"
// @Param user_id query string false "用户ID"
// @Param start_time query string false "开始时间 (YYYY-MM-DD)"
// @Param end_time query string false "结束时间 (YYYY-MM-DD)"
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(20)
// @Success 200 {object} Response{data=Pagination{list=[]SecurityEvent}}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Router /v1/security/events [get]
func GetSecurityEvents(c *gin.Context) {
	var req request.GetSecurityEventsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.GetSecurityEvents(c, &user.GetSecurityEventsReq{
		EventType:  req.EventType,
		Severity:   req.Severity,
		Status:     req.Status,
		UserId:     uint64(req.UserID),
		Pagination: nil,
	})
	if err != nil {
		response.SrvError(c, "获取安全事件列表失败", err.Error())
		return
	}
	response.Success(c, "获取安全事件列表成功", events)

}

// GetSecurityEvent 获取单个安全事件详情
// @Summary 获取单个安全事件详情
// @Description 根据事件ID获取事件详情
// @Tags Security
// @Accept json
// @Produce json
// @Param id path string true "事件ID"
// @Success 200 {object} Response{data=SecurityEvent}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Router /v1/security/events/{id} [get]
func GetSecurityEvent(c *gin.Context) {
	var req request.GetSecurityEventRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.GetSecurityEvent(c, &user.GetSecurityEventReq{
		EventId: uint64(req.EventId),
	})
	if err != nil {
		response.SrvError(c, "获取安全事件列表失败", err.Error())
		return
	}
	response.Success(c, "获取单个安全事件详情成功", events)
}

// HandleSecurityEvent 处理安全事件
// @Summary 处理安全事件
// @Description 标记安全事件为已处理或忽略
// @Tags Security
// @Accept json
// @Produce json
// @Param id path string true "事件ID"
// @Param status body map[string]string true "处理状态"
// @Success 200 {object} Response{data=SecurityEvent}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Router /v1/security/events/{id}/handle [put]
func HandleSecurityEvent(c *gin.Context) {
	var req request.HandleSecurityEventRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.HandleSecurityEvent(c, &user.HandleSecurityEventReq{
		EventId: uint64(req.EventId),
		Notes:   req.Notes,
		Handler: req.HandlerID,
	})
	if err != nil {
		response.SrvError(c, "处理安全事件失败", err.Error())
		return
	}
	response.Success(c, "处理安全事件成功", events)
}

// GetSecuritySettings 获取安全设置
// @Summary 获取安全设置
// @Description 获取系统安全设置
// @Tags Security
// @Accept json
// @Produce json
// @Success 200 {object} Response{data=SecuritySetting}
// @Failure 401 {object} ErrorResponse
// @Router /v1/security/settings [get]
func GetSecuritySettings(c *gin.Context) {
	var req request.GetSecuritySettingsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.GetSecuritySettings(c, &user.GetSecuritySettingsReq{
		UserId: uint64(req.UserId),
	})
	if err != nil {
		response.SrvError(c, "获取安全设置失败", err.Error())
		return
	}
	response.Success(c, "获取安全设置成功", events)
}

// UpdateSecuritySettings 更新安全设置
// @Summary 更新安全设置
// @Description 更新系统安全设置
// @Tags Security
// @Accept json
// @Produce json
// @Param settings body SecuritySetting true "安全设置信息"
// @Success 200 {object} Response{data=SecuritySetting}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Router /v1/security/settings [put]
func UpdateSecuritySettings(c *gin.Context) {
	var req request.UpdateSecuritySettingsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.UpdateSecuritySettings(c, &user.UpdateSecuritySettingsReq{})
	if err != nil {
		response.SrvError(c, "更新安全设置失败", err.Error())
		return
	}
	response.Success(c, "更新安全设置成功", events)
}

// GetSecurityStatus 获取安全状态概览
// @Summary 获取安全状态概览
// @Description 获取系统安全状态概览
// @Tags Security
// @Accept json
// @Produce json
// @Success 200 {object} Response{data=SecurityStatus}
// @Failure 401 {object} ErrorResponse
// @Router /v1/security/status [get]
func GetSecurityStatus(c *gin.Context) {
	var req request.GetSecurityStatusRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	var events, err = config.UserClient.GetSecurityStatus(c, &user.EmptyReq{})
	if err != nil {
		response.SrvError(c, "获取安全状态失败", err.Error())
		return
	}
	response.Success(c, "获取安全状态成功", events)
}

// GetUserSecurityInfo 获取用户安全信息
// @Summary 获取用户安全信息
// @Description 获取指定用户的安全信息
// @Tags Security
// @Accept json
// @Produce json
// @Param user_id path string true "用户ID"
// @Success 200 {object} Response{data=UserSecurityInfo}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Router /v1/security/users/{user_id} [get]
func GetUserSecurityInfo(c *gin.Context) {
	var req request.GetUserSecurityInfoRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.GetUserSecurityInfo(c, &user.GetUserSecurityInfoReq{
		UserId: uint64(req.UserID),
	})
	if err != nil {
		response.SrvError(c, "获取用户安全信息失败", err.Error())
		return
	}
	response.Success(c, "获取用户安全信息成功", events)
}

// GetRiskUsers 获取风险用户列表
// @Summary 获取风险用户列表
// @Description 获取系统中的风险用户列表
// @Tags Security
// @Accept json
// @Produce json
// @Param risk_level query string false "风险等级: low, medium, high"
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(20)
// @Success 200 {object} Response{data=Pagination{list=[]UserSecurityInfo}}
// @Failure 400 {object} ErrorResponse
// @Failure 401 {object} ErrorResponse
// @Router /v1/security/risk_users [get]
func GetRiskUsers(c *gin.Context) {
	var req request.GetRiskUsersRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	events, err := config.UserClient.GetRiskUsers(c, &user.GetRiskUsersReq{
		RiskLevel: req.RiskLevel,
		Page:      int32(req.Page),
		PageSize:  int32(req.PageSize),
	})
	if err != nil {
		response.SrvError(c, "获取风险用户列表失败", err.Error())
		return
	}
	response.Success(c, "获取风险用户列表成功", events)
}
